package com.ma4567.remotecontroller.SmallTools.Calendar;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.MysqlHelper.CalendarTodo;
import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.ffmpegManager.ffmpegController;
import com.ma4567.remotecontroller.util.CustomRespone;
import com.ma4567.remotecontroller.util.Matters;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.ArrayList;

@CrossOrigin
@RestController
public class InsertNewTodo {
    @RequestMapping(value = "/api/insertTodo", method = RequestMethod.GET)
    public String InsertTodo(HttpServletRequest request,
                              @RequestParam("cookie") String Cookie,
                              @RequestParam("gmt") String GMT,
                              @RequestParam("time") String time,
                              @RequestParam("matter") String matter) throws SQLException {
        ffmpegController.StopStream();
        TableRequestLog.AddLog("Insert new todolist " + matter, Cookie, request.getRemoteAddr());
        if(!RemoteControllerApplication.userManager.JudgeCookie(Cookie)){//鉴cookie
            return JSONObject.toJSONString(new CustomRespone(1), SerializerFeature.PrettyFormat);
        }
        else{
            time = time.split(" ")[4];
            int year = Integer.parseInt(GMT.split(" ")[3]);
            int day = Integer.parseInt(GMT.split(" ")[2]);
            int month = 0;
            switch (GMT.split(" ")[1]) {
                case "Jan" -> month = 1;
                case "Feb" -> month = 2;
                case "Mar" -> month = 3;
                case "Apr" -> month = 4;
                case "May" -> month = 5;
                case "Jun", "June" -> month = 6;
                case "Jul" -> month = 7;
                case "Aug" -> month = 8;
                case "Sep" -> month = 9;
                case "Oct" -> month = 10;
                case "Nov" -> month = 11;
                case "Dec" -> month = 12;
            }
            CalendarTodo.addMatters(RemoteControllerApplication.userManager.getUserByCookie(Cookie).Account, year, month, day, time, matter);
            return JSONObject.toJSONString(new CustomRespone(0), SerializerFeature.PrettyFormat);
        }
    }
}
